﻿using System.Linq;
using System.ComponentModel.Composition;
using Microsoft.Office.Interop.Word;

namespace Songhay.Office2010.WordWalkingStick.Commands
{
    using Songhay;
    using Songhay.Models;
    using Songhay.Office2010.Word;

    /// <summary>
    /// Customizes Office Word styles,
    /// for Quick Styles and HTML markup.
    /// </summary>
    [Export(typeof(IOfficeWordCommand))]
    public class CustomizeStylesCommand : IOfficeWordCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CustomizeStylesCommand"/> class.
        /// </summary>
        public CustomizeStylesCommand()
        {
            this.Name = "CustomizeStylesCommand";
        }

        #region IOfficeWordCommand Members

        /// <summary>
        /// Gets or sets the name
        /// (non-unique key)
        /// for locating this command
        /// in a collection of commands.
        /// </summary>
        /// <value>The name.</value>
        /// <remarks>
        /// Conventional command name prefixes:
        /// Startup* Commands with this prefix will be executed during the ThisAddIn_Startup event.
        /// Shutdown* Commands with this prefix will be executed during the ThisAddIn_Shutdown event.
        /// </remarks>
        public string Name { get; set; }

        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="wordApplication">The word application.</param>
        /// <param name="message">The message.</param>
        public void ExecuteCommand<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            var normal = wordApplication.NormalTemplate.OpenAsDocument();
            var activeDocument = wordApplication.ActiveDocument;

            Style style;

            style = normal.Styles[WdBuiltinStyle.wdStyleEmphasis];
            style.QuickStyle = false;

            style = normal.Styles[WdBuiltinStyle.wdStyleHtmlCite];
            style.AddKeyBinding(wordApplication.BuildKeyCode(WdKey.wdKeyAlt, WdKey.wdKeyT));
            style.Font.ColorIndex = WdColorIndex.wdDarkYellow;
            style.Font.Shading.BackgroundPatternColor = WdColor.wdColorAutomatic;
            style.NoProofing = 0;
            style.QuickStyle = true;
            style.UnhideWhenUsed = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleHtmlCode];
            style.AddKeyBinding(wordApplication.BuildKeyCode(WdKey.wdKeyAlt, WdKey.wdKeyC));
            style.Font.ColorIndex = WdColorIndex.wdDarkBlue;
            style.Font.Shading.BackgroundPatternColor = WdColor.wdColorLightGreen;
            style.NoProofing = 1;
            style.QuickStyle = true;
            style.UnhideWhenUsed = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleHtmlDfn];
            style.AddKeyBinding(wordApplication.BuildKeyCode(WdKey.wdKeyAlt, WdKey.wdKeyD));
            style.Font.ColorIndex = WdColorIndex.wdDarkRed;
            style.Font.Shading.BackgroundPatternColor = WdColor.wdColorAutomatic;
            style.NoProofing = 0;
            style.QuickStyle = true;
            style.UnhideWhenUsed = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleHtmlKbd];
            style.AddKeyBinding(wordApplication.BuildKeyCode(WdKey.wdKeyAlt, WdKey.wdKeyK));
            style.QuickStyle = true;
            style.UnhideWhenUsed = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleHtmlPre];
            style.AddKeyBinding(wordApplication.BuildKeyCode(WdKey.wdKeyAlt, WdKey.wdKeyP));
            style.Font.ColorIndex = WdColorIndex.wdDarkBlue;
            style.Font.Shading.BackgroundPatternColor = WdColor.wdColorAutomatic;
            style.NoProofing = 1;
            style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            style.ParagraphFormat.Shading.BackgroundPatternColor = WdColor.wdColorLightGreen;
            style.ParagraphFormat.LineUnitAfter = 1;
            style.QuickStyle = true;
            style.UnhideWhenUsed = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleHtmlSamp];
            style.AddKeyBinding(wordApplication.BuildKeyCode(WdKey.wdKeyAlt, WdKey.wdKeyM));
            style.NoProofing = 1;
            style.QuickStyle = true;
            style.UnhideWhenUsed = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleHtmlVar];
            style.AddKeyBinding(wordApplication.BuildKeyCode(WdKey.wdKeyAlt, WdKey.wdKeyV));
            style.Font.ColorIndex = WdColorIndex.wdDarkBlue;
            style.Font.Shading.BackgroundPatternColor = WdColor.wdColorLightGreen;
            style.NoProofing = 1;
            style.QuickStyle = true;
            style.UnhideWhenUsed = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleIntenseQuote];
            style.QuickStyle = false;

            style = normal.Styles[WdBuiltinStyle.wdStyleIntenseEmphasis];
            style.QuickStyle = false;

            style = normal.Styles[WdBuiltinStyle.wdStyleIntenseReference];
            style.QuickStyle = false;

            style = normal.Styles[WdBuiltinStyle.wdStyleListBullet];
            style.QuickStyle = true;

            style = normal.Styles[WdBuiltinStyle.wdStyleNormal];
            style.ParagraphFormat.LineUnitAfter = 1;
            style.QuickStyle = true;

            //FUNKYKB: "No Spacing" == WdBuiltinStyle.wdStyleNormalObject
            style = normal.Styles[WdBuiltinStyle.wdStyleNormalObject];
            style.QuickStyle = false;

            style = normal.Styles[WdBuiltinStyle.wdStyleStrong];
            style.QuickStyle = false;

            style = normal.Styles[WdBuiltinStyle.wdStyleSubtleEmphasis];
            style.QuickStyle = false;

            style = normal.Styles[WdBuiltinStyle.wdStyleSubtleReference];
            style.QuickStyle = false;

            normal.Close(SaveChanges: WdSaveOptions.wdSaveChanges,
                OriginalFormat: WdOriginalFormat.wdWordDocument,
                RouteDocument: false);

            if(activeDocument == null) return;
            activeDocument.UpdateStyles();
            activeDocument.FormattingShowFilter = WdShowFilter.wdShowFilterStylesAll;
            activeDocument.StyleSortMethod = WdStyleSort.wdStyleSortByName;
        }

        /// <summary>
        /// Executes the command for binding.
        /// </summary>
        /// <typeparam name="TMessage">The type of the message.</typeparam>
        /// <param name="wordApplication">The word application.</param>
        /// <param name="message">The message.</param>
        public object ExecuteCommandForBinding<TMessage>(Application wordApplication, TMessage message) where TMessage : IControlInteractionMessage
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }
}
